# file = '河北省优质高中期末质量检测高一语文正文.pdf'
file = '数学必修.pdf'
# file = '2021物理定稿0_15.pdf'
import io
import os
import time
import fitz
from PIL import Image
from PIL import ImageDraw

doc = fitz.Document(file)

x  = doc.load_page(30)

# def _is_pure_image(page):
#     tp = page.get_textpage()
#     # [(97, 0, 446, 111, 8, 'DeviceGray', '', 'Im0', 'FlateDecode')]
#     image_refs = list([(itr[0], itr[1], itr[2], itr[3]) for itr in page.get_images()])
#     if tp.extractText() == '' and len(image_refs) == 1:
#         image = image_refs[0]
#         if image[2] <= 1600 or image[3] <= 2400:  # 宽高
#             # 图片小于A5不符合提取要求
#             return False
#         else:
#             drawings = page.get_cdrawings()
#             if drawings and len(drawings) >= 1000:
#                 # 绘画量过大不适合直接提取图片
#                 return False
#         return True
#     return
#
#
# t1 = time.time()
# doc = fitz.Document(file)
# time1 = time.time()
# for page_num in range(doc.page_count):
#     page = doc.load_page(page_num)
#     imageList = page.get_images()
#     image_refs = list([itr[0] for itr in page.get_images()])
#     print('image_refs', image_refs)
#     print(page.rect, page.rect[0])
#     print(page_num, imageList)
#     text = page.get_text('text')
#     img_len = 0
#     for image in imageList:
#         print('image', image)
#         # 过滤小图片
#         if image[2] >= 300 or image[3] >= 300:
#             img_len += 1
#     if imageList and img_len == 1 and len(text) < 50:
#         # if 0:
#         print('imageList', imageList)
#     base_image = doc.extract_image(imageList[0][0])
#     image_bytes = base_image["image"]
#     pilim = Image.open(io.BytesIO(image_bytes))
#     #
#     pilim.save(f'./{0}.jpg')
#     # drawings = page.get_drawings()
#     # print('drawings', len(drawings))
#     sss = _is_pure_image(page)
#     print('sss', sss)
#     break
